# -*- coding: utf-8 -*-
from micolog_plugin import *
from model import *
import re,datetime,logging
from google.appengine.api import urlfetch

REG_BAIDU_URL = u'http://hi.baidu.com/([^/]*)'
REG_BLOG_PAGE = u'<a href=([/,"]?/[^>]*/blog/index/\d*["]?)>'
REG_BLOG_TITLE = u'<div class="tit"><a href="(/[^>]*/blog/item/[^>]*.html)" target="_blank">'
REG_ARTICLE_TITLE = u'<div class="tit">([^>,/,<]*)</div>'
REG_ARTICLE_CONTENT = u'<td><div id="blog_text" class="[^>]*" [\s\S]*?</div></td>'
REG_ARTICLE_CATAGORY = u'<a href="/[^>]*/blog/category/[^>]*>([^>]*)</a>'
REG_ARTICLE_DATETIME = u'<div class="date">([^>,/,<]*)</div>'


class baidu_move(Plugin):
    def __init__(self):
        Plugin.__init__(self,__file__)
        self.author= u"Fred"
        self.authoruri="mailto:me@fengsage.com"
        self.uri="http://fengsageblog.appspot.com"
        self.description= u"百度搬家工具"
        self.name="BaiduMove"
        self.version="0.1"

    def get(self,page):
        return self.render_content("baidu_move.html",{'msg':''})

    def post(self,page):
        baidu_url = page.param("url").strip()
#        logging.info('Prepare move : %s'%baidu_url)
        if re.findall(REG_BAIDU_URL, baidu_url):
            baidu_name = re.findall(REG_BAIDU_URL, baidu_url)[0]
            baidu_url = 'http://hi.baidu.com/%s'%baidu_name

            html = self.readPage('%s/blog'%baidu_url)
            
            blog_page_url = re.findall(REG_BLOG_PAGE,html)
            if blog_page_url==[]:
                return self.render_content("baidu_move.html",{'msg':'未找到该空间或该文章数少于1页'})
            
            min_page = int(blog_page_url[0].replace('"','').replace('/%s/blog/index/'%baidu_name,'')) #the first page
            max_page = int(blog_page_url[-1].replace('"','').replace('/%s/blog/index/'%baidu_name,'')) #the last page
            if min_page == max_page-1:
                max_page = int(blog_page_url[-2].replace('"','').replace('/%s/blog/index/'%baidu_name,'')) #he last page
            logging.info('Blog %s total page %s '%(baidu_name,max_page+1) )
            
            for page_index in range(0,max_page+1):
                page_url = '%s/blog/index/'%baidu_url+str(page_index)
                
                logging.info('*****Page %d analysis %s...'%(page_index+1,page_url))
                
                page_content = self.readPage(page_url)
                
                article_url = re.findall(REG_BLOG_TITLE, page_content)
                
#                logging.info('*****Page read %s...'%article_url)
#                article_url = self.findList(page_content, REG_BLOG_TITLE)
                for article in article_url:
                    article_url = 'http://hi.baidu.com/%s'%article[1:]
#                    article_list.append(article_url)
                    #circulating all article
                    article = self.articleBody(article_url)
#                    logging.info(article)
                    entry=Entry()
                    entry.title = article.get('article_title')
                    entry.author_name = article.get('article_author')
#                    entry.date = self.formatDate(article.article_date, '')
                    newcates=[]
                    c = Category.all().filter('name =',article.get('article_category'))
                    #判断分类是否存在
                    if c and c.count()>0:
                        newcates.append(c[0].key())
                    else:
                        cat= Category(name=article.get('article_category'))
                        cat.put()
                        newcates.append(cat.key())
                    entry.categorie_keys = newcates;
                    entry.content = article.get('article_content')
                    entry.save(True)
            return self.render_content("baidu_move.html",{'msg':u'搬家完毕!'})
                    
        else:
            return self.render_content("baidu_move.html",{'msg':''})
    
    def formatDate(self,str,format):
        return datetime.datetime.strptime(str,format)
    
    def readPage(self,url):
        '''
            Page reader
        '''
        logging.info('Read page:%s'%url)
        
        header = { 'Host':'hi.baidu.com',\
                   'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9',\
                   'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',\
                   'Accept-Language':'zh-cn,zh;q=0.5'}
        
        
        try:
            response = urlfetch.fetch(url=url,headers=header)
            
        except:
            logging.error('open exception')
            return 0
        if response.status_code == 200:
            html_content = response.content.replace('&nbsp;','').decode('gbk') #baidu use GBK encode
        return html_content
    
    
    def articleBody(self,url):
        
        article_html = self.readPage(url).replace('\n','')

        #Get blog title
        article_title = re.findall(REG_ARTICLE_TITLE, article_html)[0]        
        article_title = re.sub('&#(\d+);', '-', article_title)
        
        #Get blog time
        article_date = re.findall(REG_ARTICLE_DATETIME,article_html)[0]
        
        #Get blog category
        article_category = re.findall(REG_ARTICLE_CATAGORY,article_html)[0].replace(u'类别：','')
        #Get blog content
        article_content = re.findall(REG_ARTICLE_CONTENT,article_html)[0]\
                                .replace('id="blog_text" class="cnt"','')[12:][:-11]
        article = {'article_url':url,\
                   'article_title':article_title,\
                   'article_date':article_date,\
                   'article_category':article_category,\
                   'article_content':article_content,\
                   'article_author':self.author}
        return article